package com.tingyukeji.fastpay.step.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.tingyukeji.fastpay.R;
import com.tingyukeji.fastpay.base.BaseActivity;
import com.tingyukeji.fastpay.databinding.ActivityStepBinding;
import com.tingyukeji.fastpay.sport.view.RiseNumberTextView;
import com.tingyukeji.fastpay.step.bean.StepMessage;
import com.tingyukeji.fastpay.utils.SharedPreferencesUtils;
import com.mcxiaoke.bus.annotation.BusReceiver;

public class StepActivity extends BaseActivity implements View.OnClickListener{
    private ActivityStepBinding binding;
    private SharedPreferencesUtils sp;
    private boolean isBind = false;
    private int stepNum = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_step);
        binding.header.backLl.setOnClickListener(this);
        initData();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            stepNum = Integer.parseInt(bundle.getString("stepNum"));
        }
        sp = new SharedPreferencesUtils(StepActivity.this);
        //获取用户设置的计划锻炼步数，没有设置过的话默认7000
        String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "2000");
        //设置当前步数为0
        binding.cc.setCurrentCount(Integer.parseInt(planWalk_QTY), stepNum);
        binding.tvIsSupport.setText("最近同步:刚刚");
        animText(binding.tvRiseNum, stepNum);
    }

    @BusReceiver
    public void stepEvent(StepMessage stepMessage) {
        String planWalk_QTY = (String) sp.getParam("planWalk_QTY", "2000");
        binding.cc.setCurrentCount(Integer.parseInt(planWalk_QTY), stepMessage.getStepNum());
        binding.tvRiseNum.setText(stepMessage.getStepNum()+"");
    }

    /**
     * 给一个TextView设置一个数字增长动画
     */
    public void animText(RiseNumberTextView tv, int number) {
        // 设置数据
        tv.withNumber(number);
        // 设置动画播放时间
        tv.setDuration(1500);
        tv.start();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_ll:
                finish();
                break;
        }
    }
}
